/* 
通过Proxy+Reflect实现对象的数据劫持
*/

// 被代理的目标对象
const target = {
  name: '小浪',
  age: 22,
}

// 包含监视回调的处理器对象
const handler = {
  get: function(target, key) {
    console.log(`监视到读取${key}属性`)
    return Reflect.get(target, key)
  },
  set: function(target, key, value) {
    console.log(`监视到修改或添加${key}属性`)
    return Reflect.set(target, key, value)
  },
  deleteProperty: function(target, key) {
    console.log(`监视到删除${key}属性`)
    return Reflect.deleteProperty(target, key)
  }
}

// 创建一个代理对象
// const proxy = new Proxy(target, handler)

function reactive(target) {
  return new Proxy(target, handler)
}

const state = reactive(target)
console.log(state)
// console.log(state.name) // get
// state.name = 'tom' // set
// state.sex = '男'
delete state.name

console.log(target)
